package com.yswee.framework.widget.pulltorefresh.adapterview;

import com.yswee.framework.widget.adapterview.MListView;
import com.yswee.framework.widget.pulltorefresh.EmptyViewAccessor;
import com.yswee.framework.widget.pulltorefresh.PullToRefreshAdapterViewBase;
import com.yswee.framework.widget.pulltorefresh.PullToRefreshBase;
import com.yswee.framework.widget.pulltorefresh.PullToRefreshHeader;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;


public abstract class PullToRefreshListView<T> extends PullToRefreshAdapterViewBase<MListView<T>> {

	protected static final int FLAG_SETREFRESH = 1;
	protected static int mode = PullToRefreshBase.MODE_PULL_DOWN_TO_REFRESH;
	
	protected Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch(msg.what) {
			case FLAG_SETREFRESH:
				setRefreshing();
				break;
			}
		};
	};
	
	public abstract class InternalListView extends MListView<T> implements EmptyViewAccessor {
		
		public InternalListView(Context context) {
			super(context);
		}
		
		public InternalListView(Context context, AttributeSet attrs) {
			super(context, attrs);
		}
		
		public InternalListView(Context context, AttributeSet attrs, int defStyle) {
			super(context, attrs, defStyle);
		}

		@Override
		public void setEmptyView(View emptyView) {
			PullToRefreshListView.this.setEmptyView(emptyView);
		}
		
		@Override
		public void setEmptyViewInternal(View emptyView) {
			super.setEmptyView(emptyView);
		}

		public ContextMenuInfo getContextMenuInfo() {
			return super.getContextMenuInfo();
		}
		
		/**
		 * 是否在刷新的时候总是显示刷新头部
		 * @return true 总是显示头部，  false 在listview已经有数据渲染了才显示头部，否则不显示
		 */
		protected boolean getAlwaysShowHeaderOnRefresh() {
			return true;
		}
		
		@Override
		protected void onBeginLoad(boolean isFirstPage) {
			super.onBeginLoad(isFirstPage);
			if(isFirstPage && isDragRefreshing() && (getAlwaysShowHeaderOnRefresh() || getAdapterCount() > 0)) {
				mHandler.sendEmptyMessageDelayed(FLAG_SETREFRESH, 1000);
			} else {
				mHandler.removeMessages(FLAG_SETREFRESH);
				if(isRefreshing()) {
					onRefreshComplete();
				}
			}
		}
		
		@Override
		protected void onEndLoad(boolean isLastPage) {
			super.onEndLoad(isLastPage);
			mHandler.removeMessages(FLAG_SETREFRESH);
			if(isRefreshing()) {
				onRefreshComplete();
			}
		}
	}

	public PullToRefreshListView(Context context) {
		super(context);
		this.setDisableScrollingWhileRefreshing(false);
	}
	
	public PullToRefreshListView(Context context, int mode) {
		super(context, mode);
		this.setDisableScrollingWhileRefreshing(false);
	}

	public PullToRefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setDisableScrollingWhileRefreshing(false);
	}

	@Override
	public ContextMenuInfo getContextMenuInfo() {
		return ((InternalListView) getRefreshableView()).getContextMenuInfo();
	}

	@Override
	protected MListView<T> createRefreshableView(Context context, AttributeSet attrs) {
		setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				getRefreshableView().getFirstPage();
			}

			@Override
			public boolean canRefresh() {
				return !getRefreshableView().getIsBusy() &&
						getRefreshableView().isReadyLoadData(true);
			}
			
		});
		MListView<T> lv = createMListView(context, attrs);
		lv.setId(android.R.id.list);
		return lv;
	}
	
	protected abstract MListView<T> createMListView(Context context, AttributeSet attrs);
	
	@Override
	protected PullToRefreshHeader getLoadingLayout() {
		return new PullToRefreshHeader(getContext(), getPullMode());
	}

	@Override
	protected int getPullMode() {
		return mode;
	}
	
	/**
	 * 本地数据源增加数据
	 * @param t
	 * @return
	 */
	public boolean addData(T t) {
		return getRefreshableView().addData(t);
	}
	
	/**
	 * 本地数据源增加数据
	 * @param index
	 * @param t
	 * @return
	 */
	public boolean addData(int index, T t) {
		return getRefreshableView().addData(index, t);
	}
	
	/**
	 * 本地数据源移除数据
	 * @param t
	 * @return
	 */
	public boolean removeData(T t) {
		return getRefreshableView().removeData(t);
	}
	
	/**
	 * 本地数据源移除数据
	 * @param index
	 * @return
	 */
	public T removeData(int index) {
		return getRefreshableView().removeData(index);
	}
	
	/**
	 * 本地数据源移除全部数据
	 * @return
	 */
	public boolean clearData() {
		return getRefreshableView().clearData();
	}
	
}
